#include "stdinc.h"
#include <CEGUI.h>
#include <Ogre.h>

#include "GUI/DFWindow.h"

#include "DwarfFort/Interface.h"


using namespace GUI;



DFWindow::DFWindow(CEGUI::Window *parentSheet,CEGUI::Renderer *renderer)
:mRenderer(renderer)
		
{
	mWindow = CEGUI::WindowManager::getSingleton().loadWindowLayout("DFWindow.layout");
	parentSheet->addChildWindow(mWindow);



	initEvents();
	initRTTexture();
}


DFWindow::~DFWindow()
{
}


void DFWindow::initEvents()
{
		mWindow->subscribeEvent(CEGUI::Window::EventKeyDown,CEGUI::Event::Subscriber(
			   &DwarfFort::Interface::keyPressed,DwarfFort::Interface::getSingletonPtr())); 
		mWindow->subscribeEvent(CEGUI::Window::EventKeyUp,CEGUI::Event::Subscriber(
			   &DwarfFort::Interface::keyReleased,DwarfFort::Interface::getSingletonPtr()));
}


// set up a texture for CopyRenderer 
// maybe move the whole scene setup here?
void DFWindow::initRTTexture()
{
	    Ogre::RenderTexture *tex = Ogre::Root::getSingleton().getTextureManager()->createManual
								("DFScreen", "Default", Ogre::TEX_TYPE_2D, 1024, 1024, 0, 
									Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET)
                                   ->getBuffer()->getRenderTarget();
        Ogre::Camera *cam = Ogre::Root::getSingleton().getSceneManager("DFScene")->createCamera("DFScreenCam");

		Ogre::Viewport *v = tex->addViewport(cam);
        v->setOverlaysEnabled(false);
        v->setClearEveryFrame(true);
		v->setBackgroundColour(Ogre::ColourValue::Black);

        // CEGUI setup
		CEGUI::Texture *cTex = mRenderer->createTexture("DFScreen","");

        CEGUI::Imageset *imageSet = CEGUI::ImagesetManager::getSingleton().createImageset((CEGUI::utf8*)"DFScreenSet", cTex);
        imageSet->defineImage((CEGUI::utf8*)"DFScreenImg", 
            CEGUI::Point(0.0f, 0.0f),
            CEGUI::Size(cTex->getWidth(), cTex->getHeight()),
            CEGUI::Point(0.0f,0.0f));

        CEGUI::Window *si = CEGUI::WindowManager::getSingletonPtr()->getWindow("DFScreen");
     
        si->setProperty("Image", CEGUI::PropertyHelper::imageToString(&imageSet->getImage((CEGUI::utf8*)"DFScreenImg")));
}